package aar.test.tessocr;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.loginModel;
import aar.test.tessocr.utils.ConstantPara;
import aar.test.tessocr.utils.PrefUtils;
import aar.test.tessocr.utils.Urls;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.MediaType;
import okhttp3.RequestBody;


public class LoginActivity extends BaseActivity implements View.OnClickListener {

    public static final MediaType JSON=MediaType.parse("application/json; charset=utf-8");

    private Button sign_in_button;

    private AutoCompleteTextView phonenum;

    private EditText password;

    private Context mContext;

    private CheckBox save_phonenum;

    private CheckBox save_password;

    private boolean isSelectPass=false,isSelectPhone=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext=this;

        setContentView(R.layout.activity_login);

        initView();

    }


    private void initView(){

        save_phonenum=findViewById(R.id.save_phonenum);
        save_phonenum.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isSelectPhone=b;
            }
        });
        save_password=findViewById(R.id.save_password);
        save_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isSelectPass=b;

            }
        });


        sign_in_button=findViewById(R.id.sign_in_button);

        sign_in_button.setOnClickListener(this);

        phonenum=findViewById(R.id.phonenum);

        String pp=PrefUtils.getString(LoginActivity.this,ConstantPara.SAVE_PHONE_NUM,"");
        if(!TextUtils.isEmpty(pp)){
            phonenum.setText(pp);
            phonenum.setSelection(pp.length());
            save_phonenum.setChecked(true);

        }else{
            save_phonenum.setChecked(false);

        }

        password=findViewById(R.id.password);
        String ww=PrefUtils.getString(LoginActivity.this,ConstantPara.SAVE_PASSWORD,"");
        if(!TextUtils.isEmpty(ww)){
            password.setText(ww);
            password.setSelection(ww.length());
            save_password.setChecked(true);
        }else{
            save_password.setChecked(false);
        }






    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.sign_in_button:
                signIn();
                break;



        }

    }



  private void signIn(){

        String phnumber=phonenum.getText().toString();

        String pass=password.getText().toString();

        if(TextUtils.isEmpty(phnumber)){

            Toast.makeText(mContext,getResources().getString(R.string.error_empty_phonenum),Toast.LENGTH_LONG).show();

            return ;
        }

        if(11!=phnumber.length()){

            Toast.makeText(mContext,getResources().getString(R.string.error_invalid_phonenum),Toast.LENGTH_LONG).show();

            return ;
        }


        if(TextUtils.isEmpty(pass)){
            Toast.makeText(mContext,getResources().getString(R.string.error_empty_password),Toast.LENGTH_LONG).show();

            return ;

        }

      Map<String ,Object> map=new HashMap<>();
      map.put("mobile",phnumber);
      map.put("password",pass);
      map.put("app",2);

      Gson gg=new Gson();
      String json =gg.toJson(map);

      RequestBody body = RequestBody.create(JSON,json);
      OkGo.<LzyResponse<loginModel>>post(Urls.URL_LOGIN)//
              .tag(this)//
              .upRequestBody(body)
              .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
              .execute(new DialogCallback<LzyResponse<loginModel>>(LoginActivity.this) {
                  @Override
                  public void onSuccess(Response<LzyResponse<loginModel>> response) {

                      String save_ph=null;
                      if(null!=phonenum){
                          save_ph=phonenum.getText().toString().replaceAll(" ","");
                      }
                      if(isSelectPhone){
                          PrefUtils.setString(LoginActivity.this, ConstantPara.SAVE_PHONE_NUM,save_ph);
                      }else{
                          PrefUtils.setString(LoginActivity.this, ConstantPara.SAVE_PHONE_NUM,"");
                      }
                      String sava_pa=null;
                      if(null!=password){
                          sava_pa=password.getText().toString().replaceAll(" ","");
                      }
                     if(isSelectPass){
                          PrefUtils.setString(LoginActivity.this,ConstantPara.SAVE_PASSWORD,sava_pa);
                     }else{
                         PrefUtils.setString(LoginActivity.this,ConstantPara.SAVE_PASSWORD,"");
                     }



                      Log.e("LoginActivity","onSuccess--------------------");

                      BaseApplication.getIntance().setLoginBean(response.body().data);

//                      Log.e("MainActivity","onSuccess--------------------"+BaseApplication.getIntance().getLoginBean().getId());
//                      Log.e("MainActivity","onSuccess--------------------"+BaseApplication.getIntance().getLoginBean().getMobile());
                      BaseApplication.getIntance().setLogin(true);
                      startActivity(new Intent(LoginActivity.this, MainActivity.class));
//                      startActivity(new Intent(LoginActivity.this, ChioceCabActivity.class));
                      finish();
                  }

                  @Override
                  public void onError(Response<LzyResponse<loginModel>> response) {
                      Log.e("LoginActivity","onError---------------------");
//                      startActivity(new Intent(LoginActivity.this, MainActivity.class));
                      Toast.makeText(getApplicationContext(), getResources().getString(R.string.login_failed), Toast.LENGTH_LONG).show();


                  }
              });



  }















}

